<?php

class RmscloudController extends Controller {
   
  public function init()
  {
      if (Yii::app()->user->isGuest){
          $this->redirect('/users/signin');
      }
    if (Yii::app()->request->isAjaxRequest) {
      $this->layout = false;
    }
    parent::init();
  }


  public function actionIndex()
  {
    $this->redirect('/rmscloud/list');
  }
  

  public function actionList()
  {
      //$products = Products::model()->findAll();
      $categories = ProductsCategories::model()->getCategories();
      $companies = Companies::model()->getCompanies();
      $rmscloud = Rmscloud::model()->findAll();
      $companies = Companies::model()->getCompanies();
      //$this->render('list', array('rmscloud' => $rmscloud, 'companies' => $companies));
        $this->render('list', array('rmscloud' => $rmscloud, 'categories' => $categories, 'companies' => $companies));
  } 
  public function actionAdd()
  {
    $data = Yii::app()->request->getPost('data', false);
    $categories = ProductsCategories::model()->getCategories();
    $companies = Companies::model()->getCompanies();
    if ($data){
      $rmscloud = new Rmscloud();
      $rmscloud->company_id = (float)$data['company_id'];
      $rmscloud->file_description = $data['file_description'];
      $rmscloud->photo = $data['photo'];
      $rmscloud->save();

      $this->_photo_upload($data['photo']);

      $this->render('success');
    }else{
        $companies = Companies::model()->findAll();
      $this->render('add', array('companies' => $companies));
     }

  }

 public function actionEdit($id)
  {
    if($id){
      $data = Yii::app()->request->getPost('data', false);
      $categories = ProductsCategories::model()->getCategories();
      $companies = Companies::model()->getCompanies();
      $rmscloud = Rmscloud::model()->findByPk($id);
      if ($data){
        $old_photo = $rmscloud->photo;
        if($data['photo'] == ''){
            $rmscloud->photo = $old_photo;
        }else{
            $rmscloud->photo = $data['photo'];
            $this->_photo_upload($rmscloud->photo);
        }
        $rmscloud->company_id = (float)$data['company_id'];
        $rmscloud->file_description = $data['file_description'];
        $rmscloud->update();

        

        $this->render('success');
      }else{
        $rmscloud = Rmscloud::model()->find(array(
            'select'=>'company_id, file_description, photo',
            'condition'=>'id=:id',
            'params'=>array(':id'=>$id),
        ));
       $this->render('edit', array('rmscloud' => $rmscloud));
      }
    }else{
      $this->redirect('/rmscloud/list');
    }
  }

  public function actionDelete($id)
  {
    if ($id){
      $rmscloud = Rmscloud::model()->findByPk($id);
      if ($rmscloud){
        $rmscloud->delete();
        $this->redirect('/rmscloud/list');
      }
    }else{
      $this->redirect('/rmscloud/list');
    }
  }

  public function actionPhoto()
  {
    $uploadDir = '/public/photos/tmp/';
    //var_dump($_POST); die();

    // Set the allowed file extensions
    $fileTypes = array('jpg', 'jpeg', 'gif', 'png'); // Allowed file extensions

    //$verifyToken = md5('unique_salt' . $_POST['timestamp']);

    if (!empty($_FILES)) {
      $tempFile   = $_FILES['uploads']['tmp_name'][0];
      $uploadDir  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;

      // Validate the filetype
      $fileParts = pathinfo($_FILES['uploads']['name'][0]);
      if (in_array(strtolower($fileParts['extension']), $fileTypes)) {

        $uid = uniqid();
        $filename = $uid . '.' . $fileParts['extension'];
        $targetFile = $uploadDir . $filename;
        
        // Save the file
        move_uploaded_file($tempFile, $targetFile);
        echo CJSON::encode(array('file' => '/public/photos/tmp/'.$filename, 'filename' => $filename));
      } else {
        // The file type wasn't allowed
        echo 'Invalid file type.';
      }
    }
  }

  private function _photo_upload($filename)
  {
    $uploadDir = '/public/rmscloud/';
    $tempFile  = $_SERVER['DOCUMENT_ROOT'] . '/public/photos/tmp/' . $filename;
    $targetFile = $_SERVER['DOCUMENT_ROOT'] . $uploadDir . $filename;
    rename($tempFile, $targetFile);
    
    return $filename;
  }
  
  public function actionView($id)
    {
    if ($id){
        $companies = Companies::model()->getCompanies();
        $rmscloud = Rmscloud::model()->find(array(
            'select'=>'company_id, file_description, photo',
            'condition'=>'id=:id',
            'params'=>array(':id'=>$id),
        ));
        $this->render('view', array('rmscloud' => $rmscloud, 'companies'=>$companies));
    }else{
        $this->redirect('/rmscloud/list');
    }
}

}